<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/OL_SDK/include-openlayers-local.js"></script>
</head>

<body>
    <div id="map">

    </div>
    <script>
        var gaode = new ol.layer.Tile({
            title: "高德地图-矢量图层",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            }),
            // visible: false
        });
        var gaode_image = new ol.layer.Tile({
            title: "高德地图-影像图层",
            source: new ol.source.XYZ({
                url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}",
                wrapX: false
            })
        })
        const map = new ol.Map({
            target: "map",
            /* 设置地图图层 */
            // layers: [gaode, gaode_image],
            layers: [gaode_image, gaode],// 后者会压在前者上面
            /* view设置地图 以坐标,中心点,放大级别 */
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 4,
                //经纬度坐标系
                projection: "EPSG:4326"
            })
        })
        console.log(map, 'map')
        console.log(gaode, 'gaode')
        /* layer -setVisible可以控制图层显示-隐藏 */
        map.on("click", () => {
            console.log(gaode.getVisible(), 'gaode.getVisible()')
            if (gaode.getVisible()) {
                gaode.setVisible(false);
            } else {
                gaode.setVisible(true)
            }
            console.log(map.getLayers(), 'map.getLayers()')
        })
    </script>
</body>

</html>